<template>
  <div>
    <el-card class="account-card">
      <div slot="header">
        <span>添加账号</span>
      </div>
      <!-- 内容部分 -->
      <div class="content">
        <el-form
          :model="formData"
          status-icon
          :rules="rules"
          ref="form"
          label-width="100px"
          size="small"
          class="form"
        >
          <el-form-item label="账号" prop="account">
            <el-input
              type="text"
              v-model="formData.account"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="formData.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="用户组" prop="userGroup">
            <el-select v-model="formData.userGroup" placeholder="请选择用户组">
              <el-option label="超级管理员" value="超级管理员"></el-option>
              <el-option label="普通管理员" value="普通管理员"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submit">添加</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { checkAccount, checkPassword } from "@/utils/tool"
import { addUser } from '@/api/user'
export default {
  data() {
    return {
      formData: {
        account: "",
        password: "",
        userGroup: ''
      },
        rules: {
          account: [{ validator: checkAccount, trigger: "blur" }],
          password: [{ validator: checkPassword, trigger: "blur" }],
          userGroup: [
            { required: true, message: "请添加用户组", trigger: "blur" },
          ],
        },
    };
  },
  methods: {
    resetForm() {
      this.$refs.form.resetFields();
    },
    submit() {
      this.$refs.form.validate(async(value) => {
        if (value) {
         let res = await addUser({...this.formData});
         let { code,msg } = res.data
         if (code === 0) {
            this.$message({
              message:msg,
              type: "success",
            });
            this.$router.push({path:'/account'})
         }else {
            this.$message.error(msg);
         }
         console.log(res.data);
        } else {
          console.log(2);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-input__inner {
  width: 215px;
}
.form {
  width: 315px;
}
</style>